//
//  WaringInformationViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-6-8.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "WaringInformationViewController.h"
#import "MainViewController.h"
#import "webService.h"
#import "webConfig.h"

#import "SVProgressHUD.h"

@interface WaringInformationViewController ()

//创建子视图和控件
-(void)createTextToInput;
-(void)createSubScrollView;
-(void)createSubView1;
-(void)createSubView2;
-(void)createSubView3;

//回调方法
- (IBAction)searchBtnPressed:(UIButton *)sender;

@end

@implementation WaringInformationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        [self createTitle];
        [self createTextToInput];
        [self createSubScrollView];
        
        [self createSubView1];
        [self createSubView2];
        [self createSubView3];
        
        [self createStaticLbl];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getwaringInfor:) name:GETWARINGINFORMATIONDETAIL object:Nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //code
    [self searchBtnPressed:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 创建子视图和控件

-(void)createTextToInput
{
    //给text之前添加一个label
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 60, 30)];
    lbl.backgroundColor = [UIColor whiteColor];
    lbl.font = [UIFont  systemFontOfSize:16];
    lbl.textColor = [UIColor grayColor];
    lbl.text = @"车架号:";
    [self.view addSubview:lbl];
    
    //添加textField
    textToInput = [[UITextField alloc] initWithFrame:CGRectMake(110, 120, 140+50, 35)];
    textToInput.backgroundColor = [UIColor whiteColor];
    textToInput.borderStyle = UITextBorderStyleLine;
    [textToInput setTextAlignment:NSTextAlignmentCenter];
    textToInput.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    [textToInput setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self.view addSubview:textToInput];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    textToInput.text = modelIns.currentVinSel;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(250+50, 120, 30, 30);
    [btn setBackgroundImage:[UIImage imageNamed:@"mm_search.png"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(searchBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)createSubScrollView
{
    scrollViewDis = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 155, self.view.frame.size.width-80, self.view.frame.size.height-160)];
    scrollViewDis.backgroundColor = [UIColor whiteColor];
    scrollViewDis.contentSize = CGSizeMake(scrollViewDis.frame.size.width, scrollViewDis.frame.size.height+200);
    [self.view addSubview:scrollViewDis];
}

-(void)createSubView1
{
    UIView *viewTmp = [[UIView alloc] initWithFrame:CGRectMake(10, 10, scrollViewDis.frame.size.width-20, 300)];
    viewTmp.backgroundColor = [UIColor clearColor];
    [scrollViewDis addSubview:viewTmp];
    
    subView1 = [[SubView1ForWaringInformation alloc] initWithFrame:CGRectMake(11, 11, scrollViewDis.frame.size.width-20-2, 300-2)];
    subView1.backgroundColor = [UIColor whiteColor];
    [scrollViewDis addSubview:subView1];
}

-(void)createSubView2
{
    UIView *viewTmp = [[UIView alloc] initWithFrame:CGRectMake(10, 315, scrollViewDis.frame.size.width-20, 200)];
    viewTmp.backgroundColor = [UIColor clearColor];
    [scrollViewDis addSubview:viewTmp];
    
    subView2 = [[SubView2ForWaringInformation alloc] initWithFrame:CGRectMake(11, 316, scrollViewDis.frame.size.width-20-2, 200-2)];
    subView2.backgroundColor = [UIColor whiteColor];
    [scrollViewDis addSubview:subView2];
}

-(void)createSubView3
{
    UIView *viewTmp = [[UIView alloc] initWithFrame:CGRectMake(10, 520, scrollViewDis.frame.size.width-20, 250)];
    viewTmp.backgroundColor = [UIColor clearColor];
    [scrollViewDis addSubview:viewTmp];
    
    subView3 = [[SubView3ForWaringInformation alloc] initWithFrame:CGRectMake(11, 521, scrollViewDis.frame.size.width-20-2, 250-2)];
    subView3.backgroundColor = [UIColor whiteColor];
    [scrollViewDis addSubview:subView3];
}

#pragma mark - 回调方法

- (IBAction)searchBtnPressed:(UIButton *)sender
{
//    NSLog(@"searchBtnPressed !!!");
    
    //(1)请求数据,此处的操作是请求数据，并赋给模型类－－－待添加
    /*
     just for test
     */
    
    if ([textToInput.text length]!=17) {
    UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:@"请输入正确的17位车架号！" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [alertView show];
    }else{
    
    [SVProgressHUD showWithStatus:@"请等待...."];
    webService *web=[[webService alloc] init];
    web.methodName = GETWARINGINFORMATIONDETAIL;
    
        [web commonGetSharedRequest:GETWARINGINFORMATIONDETAIL wsparas:[NSMutableArray arrayWithObjects:@"vin",textToInput.text, nil]];
    }
    
    //(2)刷新数据
//    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
//    
//    _vinLbl.text = modelIns.vinStr;
//    _saleDateLbl.text = modelIns.saleDateStr;
//    _customerNameLbl.text = modelIns.customerNameStr;
//    _expirationDateLbl.text = modelIns.expirationDateStr;
//    [subView1 setNeedsDisplay];
//    [subView2 setNeedsDisplay];
//    [subView3 setNeedsDisplay];
}

-(void)getwaringInfor:(NSNotification*)notification{
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notification.object options:NSJSONReadingMutableLeaves error:Nil];
     [[MainMenuDataModel sharedMainMenuDataModel] jsonParseFromWaringInformationFile:theJson];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    _vinLbl.text = modelIns.vinStr;
    _saleDateLbl.text = modelIns.saleDateStr;
    _customerNameLbl.text = modelIns.customerNameStr;
    _expirationDateLbl.text = modelIns.expirationDateStr;
    [subView1 setNeedsDisplay];
    [subView2 setNeedsDisplay];
    [subView3 setNeedsDisplay];
    
    [SVProgressHUD dismiss];
}
- (IBAction)backBtnPressed:(UIButton *)sender
{

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建标题

-(void)createTitle
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 180, 50)];
    lbl.textColor = [HelpClass colorWithHexString:@"666666"];
    lbl.font = [UIFont systemFontOfSize:30];
    lbl.text = @"三包信息查询";
    [self.view addSubview:lbl];
}

//添加静态lbl
-(void)createStaticLbl
{
    //(1)
    UILabel *_vinLblPre = [[UILabel alloc] initWithFrame:CGRectMake(300+50, 90, 60, 30)];
    _vinLblPre.backgroundColor = [UIColor clearColor];
    _vinLblPre.textColor = [UIColor grayColor];
    _vinLblPre.font = [UIFont systemFontOfSize:16];
    _vinLblPre.textAlignment = UITextAlignmentRight;
    _vinLblPre.text = @"车架号:";
    
    _vinLbl = [[UILabel alloc] initWithFrame:CGRectMake(361+50, 90, 140+50, 30)];
    _vinLbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    _vinLbl.textColor = [UIColor grayColor];
    _vinLbl.font = [UIFont systemFontOfSize:16];
    _vinLbl.textAlignment = UITextAlignmentCenter;
    
    
    [self.view addSubview:_vinLblPre];
    [self.view addSubview:_vinLbl];
    
    //(2)
    UILabel *_saleDateLblPre = [[UILabel alloc] initWithFrame:CGRectMake(570+150, 90, 80, 30)];
    _saleDateLblPre.backgroundColor = [UIColor clearColor];
    _saleDateLblPre.textColor = [UIColor grayColor];
    _saleDateLblPre.font = [UIFont systemFontOfSize:16];
    _saleDateLblPre.textAlignment = UITextAlignmentRight;
    _saleDateLblPre.text = @"销售日期:";
    
    _saleDateLbl = [[UILabel alloc] initWithFrame:CGRectMake(651+150, 90, 140, 30)];
    _saleDateLbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    _saleDateLbl.textColor = [UIColor grayColor];
    _saleDateLbl.font = [UIFont systemFontOfSize:16];
    _saleDateLbl.textAlignment = UITextAlignmentCenter;
   
    
    [self.view addSubview:_saleDateLblPre];
    [self.view addSubview:_saleDateLbl];

    //(3)
    UILabel *_customerNameLblPre = [[UILabel alloc] initWithFrame:CGRectMake(300+50, 130, 60, 30)];
    _customerNameLblPre.backgroundColor = [UIColor clearColor];
    _customerNameLblPre.textColor = [UIColor grayColor];
    _customerNameLblPre.font = [UIFont systemFontOfSize:16];
    _customerNameLblPre.textAlignment = UITextAlignmentRight;
    _customerNameLblPre.text = @"车主:";
    
    _customerNameLbl = [[UILabel alloc] initWithFrame:CGRectMake(361+50, 130, 140+50, 30)];
    _customerNameLbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    _customerNameLbl.textColor = [UIColor grayColor];
    _customerNameLbl.font = [UIFont systemFontOfSize:16];
    _customerNameLbl.textAlignment = UITextAlignmentCenter;
  
    
    [self.view addSubview:_customerNameLblPre];
    [self.view addSubview:_customerNameLbl];
    
    //(4)
    UILabel *_expirationDateLblPre = [[UILabel alloc] initWithFrame:CGRectMake(510+150, 130, 140, 30)];
    _expirationDateLblPre.backgroundColor = [UIColor clearColor];
    _expirationDateLblPre.textColor = [UIColor grayColor];
    _expirationDateLblPre.font = [UIFont systemFontOfSize:16];
    _expirationDateLblPre.textAlignment = UITextAlignmentRight;
    _expirationDateLblPre.text = @"有效截止日期:";
    
    _expirationDateLbl = [[UILabel alloc] initWithFrame:CGRectMake(651+150, 130, 140, 30)];
    _expirationDateLbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    _expirationDateLbl.textColor = [UIColor grayColor];
    _expirationDateLbl.font = [UIFont systemFontOfSize:16];
    _expirationDateLbl.textAlignment = UITextAlignmentRight;
    
    
    [self.view addSubview:_expirationDateLblPre];
    [self.view addSubview:_expirationDateLbl];
}

@end
